
#include <p30f4013.h>
#include <stdlib.h>
#include <string.h>
#include "Initializations.h"
#include "Delays.h"
#include "LCD_controls.h"


//--------------------------------------------------------------------------
//		Initializations
//--------------------------------------------------------------------------


/* Used for the 18F4450 only, configures the internal oscillator */
void oscillator_init(void)
{
    
	OSCCONbits.NOSC = 1;		//FRC fast internal RC
        OSCCONbits.POST0 = 0;            //Post scaler does not affect clock
        OSCCONbits.POST1 = 0;            //Post scaler does not affect clock
        OSCCONbits.LPOSCEN = 0;         //secondary oscillator disabled
        OSCCONbits.OSWEN = 0;            //Do not switch oscillator to NOSC selection


	return;
}




/* Set up the desired interrupts and priority levels */
void interrupt_init(void)
{
    IEC0 = 0;
    IEC1 = 0;        //turn off all interrupts

	return;
}


/* Timer 0 generates an interrupt every 5 seconds */
void timer0_init(void)
{
    /*
	INTCONbits.TMR0IE = 1;		//turn on the timer 0 interrupt
	T0CON = 0x07;  				//Prescaler = 256
	TMR0H = 0x67;				//To get an interrupt every 5 seconds, the count must be
	TMR0L = 0x69;				// initialized to FFFFh - 9896h
*/
	return;
}

/* Timer 1 generates an interrupt every 0.25 seconds */
void timer1_init(void)
{
    /*
	PIE1bits.TMR1IE = 1;		//Turn on the timer 2 interrupt
	IPR1bits.TMR1IP = 1;		//Set it to high priority
	T1CON = 0x30;				//Write in one 16 bit operation, Prescaler = 8
								// internal clock source, timer off
	TMR1H = 0x0B;				//Need this count to generate a 0.25 second interrupt
	TMR1L = 0xDB;
*/
	return;
}


void timer1_reset(void)
{
    /*
	TMR1H = 0x0B;				//Need this count to generate a 0.25 second interrupt
	TMR1L = 0xDB;
     */
	return;
}




/* Timer 2 generates an interrupt every 1 milli-seconds */
void timer2_init(void)
{
    
    T2CONbits.TSIDL = 0;    //continue timer operation in idle mode
    T2CONbits.TGATE = 0;    //gated time accumulation disabled
    T2CONbits.TCKPS0 = 0;
    T2CONbits.TCKPS1 = 0;   //timer prescale is 1
    T2CONbits.T32 = 0;      //Timer 2 and 3 are separate 16-bit timers
    T2CONbits.TCS = 0;      //Timer source is Tosc/4

    PR2 = 29481;            //set the reset-count value

    T2CONbits.TON = 1;      //Turn the timer on

	return;
}

void timer2_reset(void)
{
    TMR2 = 36056;
    
            return;
}




void port_init(void)
{
    
	//ADCON0 = 0;			//Turn off the A/D converter so it doesnt use my digital I/O pins!
	//ADCON1 = 0x0F;                  //VSS and VDD are references (to free up the digital pins!)
                                        //  All digital I/O for AN pins for now

	//TRISA = 0xFF;                   //Mostly inputs
       //TRISAbits.TRISA4 = 0;              //
       //TRISAbits.TRISA5 = 0;              //
	TRISB = 0x00;  			//PORTB is outputs
        TRISBbits.TRISB12 = 1;          //This is the A/D converter input
	//TRISBbits.TRISB7 = 0;		//RB7 is an output to handle the RS pin on the LCD
	//TRISBbits.TRISB6 = 0;		//RB6 is the LCDenable pin
	TRISC = 0x00;			//Outputs for now
        TRISD = 0x00;
        TRISDbits.TRISD3 = 1;           
        TRISF = 0x00;


	PORTA = 0;
	PORTB = 0;
	PORTC = 0;					//Make sure everything is set to zero to begin with
        PORTD = 0;
        PORTF = 0;

	return;
}




void AD_init(void)
{
    ADCON1bits.ADSIDL = 1;      //Discontinue in idle mode
    ADCON1bits.FORM = 0b00;       //Data out format is integer (0000 dddd dddd dddd)
    ADCON1bits.SSRC = 0b111;    //Internal counter ends sampling and starts conversion
                                //  (auto convert)
    ADCON1bits.ASAM = 1;        //Sampling begins immediately afer last conversion
                                //  SAMP bit is auto set


    ADCON2bits.VCFG = 0b000;    //Reference is AVdd and AVss
    ADCON2bits.CSCNA = 0;       //Do not scan inputs
    ADCON2bits.SMPI = 0b0000;   //Interrupts at complesion of conversion for
                                //  each sample/convert sequence
    ADCON2bits.BUFM = 0;        //Buffer configured as one 16-word buffer
    ADCON2bits.ALTS = 0;        //Always use MUX A input multiplexer settings


    ADCON3bits.SAMC = 0b11111;  //Auto sample time = 31*T_ad
    ADCON3bits.ADRC = 0;        //A/D conversion clock derived from system clock
    ADCON3bits.ADCS = 0b111111; //A/D conversion clock = 32*T_cy  (T_cy = 4*T_osc)


    ADCHSbits.CH0NA = 0;        //Channel0 negative input is Vref-
    ADCHSbits.CH0SA = 0b1100;   //Channel0 positive input is AN12


    ADPCFG = 0b1110111111111111;    //AN12 only in Analog mode
                                    //  all other pins in Digital I/O mode




    ADCON1bits.ADON = 1;         //at the end of everything, turn on the A/D

    return;
}






